#include "client.h"

#include <QDebug>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

Client::Client(QObject *parent)
    : QObject{parent}
{

}

void Client::connecting()
{
    qDebug() << "client: start connect,";
    int m_clientFd;
    m_clientFd = ::socket(AF_INET, SOCK_STREAM, 0);
    if (m_clientFd < 0)
    {
        qDebug() << "client: create socket failed.";
        emit connectStatus(0);
    }
    struct sockaddr_in serverAddr;
    bzero(&serverAddr, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    inet_aton("127.0.0.1", &serverAddr.sin_addr);
    int err = ::connect(m_clientFd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
    if (err < 0)
    {
        qDebug() << "client: connected failed";
        emit connectStatus(0);
    }
    else
    {
        qDebug() << "client: connect successed.";
        emit connectStatus(m_clientFd);
    }
    return;
}
